package com.camera2.activity;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.camera2.bean.ApplicationHelper;
import com.camera2.bean.ExpertBean;
import com.camera2.frament.Frament;
import com.camera2.frament.FramentExpert;
import com.camera2.http.Login;
import com.camera2.http.SendExpert;
import com.camera2.local.SharedPrefer;

import java.util.ArrayList;
import java.util.List;


public class ExpertInfo extends Activity {

    private EditText et_name;
    private TextView tv_id;
    private EditText et_phone;
    private EditText et_price;
    private EditText et_introduce;

    private ExpertBean expertBean;

    private String saveUpdate;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String info = (String) msg.obj;

                    if (info.equals("success")) {
                        Toast.makeText(getApplicationContext(), "提交信息成功", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "连接网络失败,请检查网络是否连接", Toast.LENGTH_SHORT)
                            .show();
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.expert_info);

        et_name = findViewById(R.id.et_name);
        tv_id = findViewById(R.id.tv_id);
        et_phone = findViewById(R.id.et_phone);
        et_price = findViewById(R.id.et_price);
        et_introduce = findViewById(R.id.et_introduce);
//        initDate();
        getExpert();

    }

    public void initDate() {
        expertBean = ((ApplicationHelper) getApplication().getApplicationContext()).getExpertBean();
        if (expertBean != null) {
            saveUpdate = "update";
            tv_id.setText(SharedPrefer.Readid(getApplicationContext()));
            et_name.setText(expertBean.getName());
            et_phone.setText(expertBean.getPhone());
            et_price.setText(expertBean.getPrice() + "");
            et_introduce.setText(expertBean.getIntroduce());
        } else saveUpdate = "save";

    }


    public void submit(View view) {
        String id = tv_id.getText().toString().trim();
        String name = et_name.getText().toString().trim();
        String phone = et_phone.getText().toString().trim();
        String price = et_price.getText().toString().trim();
        String introduce = et_introduce.getText().toString().trim();

        if (name.equals("") || id.equals("") || phone.equals("") || price.equals("") || introduce.equals(""))
            Toast.makeText(this, "请将信息填写完整", Toast.LENGTH_SHORT).show();
        else {
            expertBean.setUserId(id);
            expertBean.setIntroduce(introduce);
            expertBean.setName(name);
            expertBean.setPhone(phone);
            expertBean.setPrice(Integer.parseInt(price));
            ((ApplicationHelper) getApplication().getApplicationContext()).setExpertBean(expertBean);
            setExpert();
        }
    }

    public void setExpert() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String log = SendExpert.send(getApplicationContext(),saveUpdate);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = log;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 0;
                    handler.sendMessage(msg);
                }
            }
        }).start();

    }

    public void getExpert() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String log = SendExpert.getExpert(getApplicationContext());
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = log;
                    handler1.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 0;
                    handler1.sendMessage(msg);
                }

            }
        }).start();

    }

    private Handler handler1 = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String info = (String) msg.obj;
                    if (info.equals("success")) {
                        initDate();
                    }
                    break;
                case 0:
                    break;
            }

        }
    };

}
